<template>
  <div>
    <Card shadow>
      <iview-table
              ref="table"
              :baseURL="queryModel.baseURL"
              :menuId="dataModel.menuId"
              :columns="dataModel.columns"
              :order="dataModel.order"
              :pageNumber="pageModel.pageNumber"
              :pageSize="pageModel.pageSize"
              :defaultFilters="queryModel.defaultFilters"
              v-on="{changeSelect:changeSelect,changeTotalSize:changeTotalSize,events:events}">
      </iview-table>
      <br>
      <iview-page
              :totalSize="pageModel.totalSize"
              :pageNumber="pageModel.pageNumber"
              :pageSize="pageModel.pageSize"
              :pageSizeOpts="pageModel.pageSizeOpts"
              v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
      </iview-page>
    </Card>
    <Modal v-model="showModal" :closable='false' :mask-closable=true :width="600">
      <h3 slot="header" style="color:#2D8CF0">会员信息</h3>
      <Form ref="selectRow" :model="selectRow" :label-width="120">
        <Row>
          <Col span="18" offset="3">
            <FormItem label="会员等级：">
              <Select v-model="selectRow.levelId" clearable :label-in-value=true @on-change="changeLevel">
                <Option v-for="item in levels" :value="item.id" :key="item.id">{{ item.levelName }}{{item.level}}</Option>
              </Select>
            </FormItem>
            <FormItem label="储值账户：">
              <InputNumber style="width: 100%" :min="0" v-model="selectRow.storeAccount"></InputNumber>
            </FormItem>
            <FormItem label="赠送账户：">
              <InputNumber style="width: 100%" :min="0" v-model="selectRow.giftAccount"></InputNumber>
            </FormItem>
            <FormItem label="疗程账户：">
              <InputNumber style="width: 100%" :min="0" v-model="selectRow.courseAccount"></InputNumber>
            </FormItem>
            <FormItem label="累计充值金额：">
              <InputNumber style="width: 100%" :min="0" v-model="selectRow.totalStoreAmount"></InputNumber>
            </FormItem>
            <FormItem label="累计赠送金额：">
              <InputNumber style="width: 100%" :min="0" v-model="selectRow.totalGiftAmount"></InputNumber>
            </FormItem>
            <FormItem label="累计消费金额：">
              <InputNumber style="width: 100%" :min="0" v-model="selectRow.totalConsumeAmount"></InputNumber>
            </FormItem>
            <FormItem label="信誉分：">
              <InputNumber style="width: 100%" :min="0" v-model="selectRow.score"></InputNumber>
            </FormItem>
          </Col>
        </Row>
      </Form>
      <div slot="footer">
        <Button type="info" size="large" @click="save">保存</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    export default {
        components: {
            iviewTable,
            iviewPage
        },
        props: {
            menuId: {
                type: [Number, String],
                default: 1100
            },
            level: {
                type: [Number, String],
                default: -1
            }
        },
        data () {
            return {
                showModal: false,
                baseURL: '/user/member',
                selectRow: {},
                selectRows: [],
                levels: [],
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/member',
                    defaultFilters: [
                    ]
                },
                dataModel: {
                    menuId: this.menuId,
                    columns: [
                        {
                            type: 'selection',
                            width: 60,
                            align: 'center'
                        },{
                            title: '会员姓名',
                            key: 'memberName'
                        },{
                            title: '会员编号',
                            key: 'memberNumber'
                        },{
                            title: '会员等级',
                            key: 'levelName'
                        },{
                            title: '储值账户',
                            key: 'storeAccount'
                        },{
                            title: '疗程账户',
                            key: 'courseAccount'
                        },{
                            title: '赠送账户',
                            key: 'giftAccount'
                        },{
                            title: '累计充值',
                            key: 'totalStoreAmount'
                        },{
                            title: '累计消费',
                            key: 'totalConsumeAmount'
                        },{
                            title: '信用分',
                            key: 'score'
                        },{
                            title: '会员状态',
                            key: 'status',
                            render: (h, params) => {
                                let temp = params.row.status
                                if (temp === 0) {
                                    temp = '激活'
                                } else if (temp === 1) {
                                    temp = '冻结'
                                }
                                return h('span', temp)
                            }
                        }
                    ],
                    order: 'creation_time desc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [10, 50, 100, 500]
                }
            }
        },
        methods: {
            events (item) {
                if (item.path === 'edit') {
                    this.edit()
                }
            },
            loadLevel () {
                let url = this.baseURL + '/level/findAll'
                this.$ajax.get(url).then((response) => {
                    let {code, data, msg} = response.data
                    if (code === 0) {
                        this.levels = data
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            changeLevel (option) {
                this,selectRow.levelId = option.value
                this,selectRow.levelName = option.label
            },
            edit () {
                if (this.selectRows.length === 1) {
                    this.loadLevel()
                    this.selectRow = this.selectRows[0]
                    this.showModal = true
                } else {
                    this.$bus.alert('warning', '请选择一条记录')
                }
            },
            save () {
                let url = this.baseURL + '/saveOrUpdate'
                this.$ajax.post(url, this.selectRow).then(r => {
                    let {code, msg} = r.data
                    if (code === 0) {
                        this.$bus.alert('success', '已保存')
                        this.showModal = false
                        this.refresh()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            refresh () {
                this.queryModel.defaultFilters.length = 0
                if (this.level === 1) {
                    this.queryModel.defaultFilters.push({'field': 'total_store_amount', 'value1': 50000, 'operate': '<'})
                } else if (this.level === 2) {
                    this.queryModel.defaultFilters.push({'field': 'total_store_amount', 'value1': 50000, 'operate': '>='})
                    this.queryModel.defaultFilters.push({'field': 'total_store_amount', 'value1': 100000, 'operate': '<='})
                } else if (this.level === 3) {
                    this.queryModel.defaultFilters.push({'field': 'total_store_amount', 'value1': 100000, 'operate': '>'})
                }
                this.$refs.table.refresh()
            },
            changeSelect (selection) {
                this.selectRows = selection
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            },
            init () {
                this.refresh()
            }
        },
        mounted () {
            this.init()
        },
        watch: {
            '$route' (to) {
                if (to.path === '/member/list') {
                    this.init()
                }
            }
        }
    }
</script>

<style>

</style>
